var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    showSealAction:false,
    showUpdateDialog:false,
    company:null,
    sealList:[],
    deleteSealList:[],
    sealGroupList:[],
    departmentList:[],
    checkDepartmentId:0,
    statusText:['已删除','已停用','已绑定'],
    sealActionButtons:[{text:'修改名称',value:'edit'},{text:'移动到部门',value:'move'},{text:'删除印章',type: 'warn',value:'remove'}],
    sealActionButtons1:[{text:'修改名称',value:'edit'},
      {text:'移动到部门',value:'move'},
      {text:'停用印章',value:'stopUsing'},
      {text:'删除印章',type: 'warn',value:'remove'}
    ],
    sealActionButtons2:[{text:'修改名称',value:'edit'},
      {text:'移动到部门',value:'move'},
      {text:'启用印章',value:'startUsing'},
      {text:'删除印章',type: 'warn',value:'remove'}
    ],

    selectedSeal:null,

    sealName:'',

    showCheckDialog:false,

    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '印章列表', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad() {
    requestClient = new ZYRequestClient();
  },
  // 加载印章列表
  loadSealList:function(){
    var _this = this;
    var defaultCompany = wx.getStorageSync('defaultCompany');
    if(defaultCompany == ''){
      defaultCompany = { id:0,name:'无' };
    }
    _this.setData({
      company:defaultCompany
    });
    requestClient.getSealList2(defaultCompany.id,function(res){
      if(res.data.data.length > 0){
        var sealList = [];
        var deleteSealList = [];
        for(var i=0;i<res.data.data.length;i++){
          var seal = res.data.data[i];
          if(seal.status == 0){
            deleteSealList.push(seal);
          } else {
            sealList.push(seal);
          }
        }
        _this.setData({
          sealGroupList:[],
          sealList:sealList,
          deleteSealList:deleteSealList,
        });
        // 存储到离线缓存
        wx.setStorageSync('deleteSealList', deleteSealList);
      }
      // 重组数据
      if(_this.data.sealList.length > 0){
        var sealList = _this.data.sealList;
        for(var i =0;i < sealList.length;i++){
          var seal = sealList[i];
          var departmentId = seal.departmentId;
          if(!_this.checkDuplicate(departmentId)){
            var sealGroupList = _this.data.sealGroupList;
            var sealGroup = {
              id:departmentId,
              name:departmentId == 0?'':seal.departmentVo.name
            };
            sealGroupList.push(sealGroup);
            _this.setData({
              sealGroupList:sealGroupList
            });
          }
        }
      }
    });
  },
  // 加载公司一级部门
  loadDefaultCompany(){
    var _this = this;
    var defaultCompany = wx.getStorageSync('defaultCompany')
    if(defaultCompany == ''){
      defaultCompany = { id:0 };
    }
    requestClient.getCompanyResource(defaultCompany.id,function(resource){
    _this.setData({
      departmentList:resource.data.data.departmentVoList
    });
  });
  },
  // 判断是否重复
  checkDuplicate(departmentId){
    var _this = this;
    var sealGroupList = _this.data.sealGroupList;
    for(var i=0;i<sealGroupList.length;i++){
      var sealGroup = sealGroupList[i];
      if(sealGroup.id == departmentId){
        return true;
      }
    }
    return false;
  },
  onShow(){
    var _this = this;
    _this.setData({
      sealList:[]
    });
    requestClient.checkFunctionPermission(1001,'您没有权限查看印章列表，请联系企业创建者！',function(){
      _this.loadSealList();
    },function(){
      wx.navigateBack({
        delta: 1,
      })
    });
  },
  // 查看删除印章 
  bindScanDeleteSealList(e){
    var _this = this;
    if(_this.data.deleteSealList.length == 0){
      return;
    }
    wx.navigateTo({
      url: '../removeseallist/index',
    })
  },
  // 跳转印章详情页面
  bindSealClick(e){
    var seal = e.currentTarget.dataset.seal;
    // 如果删除则不能查看
    if(seal.status == 0){
      return;
    }
    requestClient.checkFunctionPermission(1001,'您没有权限查看印章列表，请联系企业创建者！',function(){
      wx.navigateTo({
        url: '../sealinfo/index?id=' + seal.id + '&editable=true',
      })
    },function(){
      wx.navigateBack({
        delta: 1,
      })
    });
  },
  // 长按显示操作菜单
  bindSealLongPress(e){
    var _this = this;
    var seal = e.currentTarget.dataset.seal;
    // 如果删除则不显示菜单
    if(seal.status == 0){
      return;
    }
    if(seal.status == 1){
      _this.setData({
        sealActionButtons:_this.data.sealActionButtons2,
      });
    }
    if(seal.status == 2){
      _this.setData({
        sealActionButtons:_this.data.sealActionButtons1,
      });
    }
    _this.setData({
      selectedSeal:seal,
      sealName:seal.name,
      showSealAction:true
    });
  },
  // 输入印章名称
  formUpdateInputChange(e){
    this.setData({
      sealName:e.detail.value
    });
  },
  // 点击操作菜单按钮事件
  bindSealActionTap(e){
    var _this = this;
    var seal = _this.data.selectedSeal;
    _this.setData({
      showSealAction:false
    });
    if(e.detail.value == 'edit'){
        _this.setData({
          showUpdateDialog:true
        });
    } 
    else if(e.detail.value == 'move'){
      _this.setData({
        showCheckDialog:true,
        checkDepartmentId:0,
        departmentList:[]
      });
      _this.loadDefaultCompany();
    } 
    else if(e.detail.value == 'startUsing'){
      requestClient.checkFunctionPermission(1009,'您没有权限启用印章，请联系企业创建者！',function(){
          requestClient.updateSealStatus(seal.id,2,function(res){
            _this.loadSealList();
          })
      });
    }
    else if(e.detail.value == 'stopUsing'){
      requestClient.checkFunctionPermission(1009,'您没有权限停用印章，请联系企业创建者！',function(){
          wx.showModal({
          title:'停用印章',
          content:'您确定要停用此印章吗？停用后将不能申请用印，只能有权限的管理员使用！',
          success(res){
            if(res.confirm){
              requestClient.updateSealStatus(seal.id,1,function(res){
                _this.loadSealList();
              })
            }
          }
        })
      });
    }
    else {
      requestClient.checkFunctionPermission(1003,'您没有权限删除印章，请联系企业创建者！',function(){
          wx.showModal({
          title:'删除印章',
          content:'您确定要将当前的印章从此企业删除吗',
          success(res){
            if(res.confirm){
              var userInfo = wx.getStorageSync('userInfo');
              requestClient.removeSeal(userInfo.id,seal.id,function(res){
                _this.loadSealList();
              })
            }
          }
        })
      });
    }
  },
  // 更新印章名称
  bindUpdateSeal(e){
    var _this = this;
    _this.setData({showUpdateDialog:false});
    if(e.detail.index == 1){
      if(_this.data.sealName == ''){
        wx.showToast({
          title: '名称不能为空！',
          icon:'error'
        })
        return;
      }
      requestClient.checkFunctionPermission(1007,'您没有权限编辑印章，请联系企业创建者！',function(){
        // 提交更新
        requestClient.updateSealName(_this.data.selectedSeal.id,_this.data.sealName,function(res){
          _this.loadSealList();
        })
      });
    }
  },
  // 选择部门
  departmentCheckboxChange(e){
    this.setData({
      checkDepartmentId:parseInt(e.detail.value)
    });
  },
  // 关闭选择
  bindClosedDepartment(e){
    this.setData({
      checkDepartmentId:0
    });
  },
  // 确认选择部门
  bindSelectedDepartmentClick(e){
    var _this = this;
    _this.setData({
      showCheckDialog:false
    });
    // 判断是否选中了部门
    if(_this.data.checkDepartmentId !=0){
      requestClient.checkFunctionPermission(1008,'您没有权限移动印章到部门，请联系企业创建者！',function(){
        requestClient.updateSealDepartment(_this.data.selectedSeal.id,_this.data.checkDepartmentId,function(res){
          if(res.data.data){
            _this.loadSealList();
          }
        });
      });
    }
  }
})